package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.ProductBrand;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.ProductBrandRepository;
import co.yixiang.modules.shop.service.ProductBrandService;
import co.yixiang.modules.shop.service.dto.ProductBrandDto;
import co.yixiang.modules.shop.service.dto.ProductBrandQueryCriteria;
import co.yixiang.modules.shop.service.mapper.ProductBrandMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijunze
* @date 2020-04-28
*/
@Service
//@CacheConfig(cacheNames = "productBrand")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProductBrandServiceImpl implements ProductBrandService {

    private final ProductBrandRepository productBrandRepository;

    private final ProductBrandMapper productBrandMapper;

    public ProductBrandServiceImpl(ProductBrandRepository productBrandRepository, ProductBrandMapper productBrandMapper) {
        this.productBrandRepository = productBrandRepository;
        this.productBrandMapper = productBrandMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ProductBrandQueryCriteria criteria, Pageable pageable){
        Page<ProductBrand> page = productBrandRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(productBrandMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ProductBrandDto> queryAll(ProductBrandQueryCriteria criteria){
        return productBrandMapper.toDto(productBrandRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ProductBrandDto findById(Integer id) {
        ProductBrand productBrand = productBrandRepository.findById(id).orElseGet(ProductBrand::new);
        ValidationUtil.isNull(productBrand.getId(),"ProductBrand","id",id);
        return productBrandMapper.toDto(productBrand);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ProductBrandDto create(ProductBrand resources) {
        return productBrandMapper.toDto(productBrandRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ProductBrand resources) {
        ProductBrand productBrand = productBrandRepository.findById(resources.getId()).orElseGet(ProductBrand::new);
        ValidationUtil.isNull( productBrand.getId(),"ProductBrand","id",resources.getId());
        productBrand.copy(resources);
        productBrandRepository.save(productBrand);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            productBrandRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProductBrandDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProductBrandDto productBrand : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("品牌名称", productBrand.getName());
            map.put("品牌描述", productBrand.getDescription());
            map.put("品牌图片", productBrand.getImage());
            map.put("删除状态,0未删除，1已删除", productBrand.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}